package com.xui.android.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.xuexiang.xui.widget.imageview.ImageLoader
import com.xuexiang.xui.widget.toast.XToast
import com.xui.android.R
import kotlinx.android.synthetic.main.iv_item.view.*

class BannerAdapter(private val imageList: List<String>) : RecyclerView.Adapter<BannerAdapter.BannerViewHolder>() {

    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivItemImage: ImageView =view.iv_item//获取布局中的ImageView
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view =LayoutInflater.from(parent.context)
            .inflate(R.layout.iv_item,parent,false)
        val bannerViewHolder = BannerViewHolder(view)

        bannerViewHolder.itemView.setOnClickListener{
            val position = bannerViewHolder.adapterPosition
            XToast.normal(parent.context, "轮播到了第 $position 个").show();
        }
        return bannerViewHolder
    }

    override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
        val imageView  = holder.ivItemImage
        //XUI 的图片加载器
        ImageLoader.get().loadImage(imageView, imageList[position]);
    }
    override fun getItemCount(): Int {
        return imageList.size
    }
}
